package org.blogwu.learn.multithread.chapter3.demo9;

import java.util.concurrent.locks.LockSupport;

/**
 * 此示例是吧  把suspend的案例使用support来进行修改完善.
 *
 * @author hyf
 * @date 2018-09-23
 **/
public class Main1 {
    public static void main(String[] args) throws InterruptedException {
        LockSupportThreadTest lockSupportThreadTest1 = new LockSupportThreadTest("s1");
        LockSupportThreadTest lockSupportThreadTest2 = new LockSupportThreadTest("s2");
        lockSupportThreadTest1.start();
        Thread.sleep(1000);
        lockSupportThreadTest2.start();
        LockSupport.unpark(lockSupportThreadTest2);
        System.out.println("解锁 t2");
        Thread.sleep(3000);
        LockSupport.unpark(lockSupportThreadTest1);
        lockSupportThreadTest1.join();
        lockSupportThreadTest2.join();
    }
}

class LockSupportThreadTest extends Thread {
    static Object o1 = new Object();

    public LockSupportThreadTest(String name) {
        super(name);
    }

    @Override
    public void run() {
        synchronized (o1) {
            System.out.println("in " + getName());
            LockSupport.park();
        }
        System.out.println("over " + getName());
    }
}
